package homework.tcp;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 线程管理连接的Socket，并进行中转
 */
public class Server implements Runnable {

    //服务器
    private static ServerSocket serverSocket;

    //端口号
    private static final int port = 9999;

    //客户端的socket
    public Server() {}

    static {
        try {
            //创建服务器 端口9999
            serverSocket = new ServerSocket(port);
            System.out.println("端口为" + port + "服务器已启动.....");
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    @Override
    public void run() {
        Socket socket = null;
        try {
            while (true) {
                //挂起等待客户端连接
                socket = serverSocket.accept();
                new Thread(new Forward(socket)).start();
            }
        } catch (IOException e) {
            System.out.println("服务器主线程开小差了.....");
        }

    }


}
